<template>
  <!-- 报警设置 -->
  <div class="dialog-container">
    <el-dialog
      title="配置报警类型"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
      >
        <el-row>
          <el-col>
            <el-checkbox-group v-model="typeValue">
              <el-checkbox v-for="(item, index) in callPoliceList" :key="index + '_alarm'" :label="item.code" :value="item.code">{{ item.codeName }}</el-checkbox>
            </el-checkbox-group>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button class="cancel-btn" size="mini" @click="handleCheckAll">全选</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from '@/utils/dict'
import { edit } from '@/api/system/roles'
export default {
  name: 'AddCallPoliceDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      callPoliceList: [],
      dataForm: {
        id: '',
        alarmData: ''
      },
      typeValue: [],
      rules: {}
    }
  },
  created() {
    this.callPoliceList = getDict('e001')
  },
  mounted() {},
  methods: {
    initData() {
      this.saveLoading = false
      if (this.obj) {
        const obj = JSON.parse(JSON.stringify(this.obj))
        this.dataForm = obj
        this.typeValue = obj.alarmData ? obj.alarmData.split(',') : []
      }
    },
    handleSave() {
      this.dataForm.alarmData = this.typeValue.toString()
      const param = JSON.parse(JSON.stringify(this.dataForm))
      console.log(param)
      edit(param).then(() => {
        this.$message.success('保存成功')
        this.$emit('closeDialog')
        this.$emit('flushPage')
      }).catch(() => {
        this.$message.error('保存失败')
      })
    },
    handleCheckAll() {
      this.typeValue = this.callPoliceList.map(item => item.code)
    },
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
